home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / pump_src / tph.c < prev    next >
C/C++ Source or Header  |  1995-10-26  |  7KB  |  312 lines

  1.  
  2. #include "pump.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <conio.h>
  9. #include <vga.h>
  10. #include <sincos.h>
  11. #include <llkey.h>
  12. #include <jclib.h>
  13. #include <cspline.h>
  14. #include <vbl.h>
  15. #include "types.h"
  16. #include "phong.h"
  17.  
  18. #define TRACE(r,g,b) do { if (tracing) VGA_PutColor(0,r,g,b); } while(0)
  19.  
  20. PRIVATE CSP_PSpline spl_x, spl_y, spl_z, spl_rot, spl_tumble;
  21. PRIVATE double t;           // Tiempo
  22. void *env_map_adr;
  23. int tracing = 0;
  24.  
  25. static void *envpix, *envpal, *o3d, *virtscr;
  26.  
  27. void DumpScr(void) {
  28. #if 0
  29.     int i;
  30.     byte *p = (byte *)0xA0000;
  31.     byte *q = virtscr;
  32.  
  33.     for (i = 0; i < 200; i+=2) {
  34.         memcpy(p, q, 320);
  35.         p += 640;
  36.         q += 640;
  37.     }
  38. #else
  39.     memcpy((void *)0xA0000, virtscr, 64000);
  40. #endif
  41. }
  42.  
  43. PRIVATE UWORD rot = 0;
  44. PRIVATE WORD z = 512, x = 0, y = 0;
  45. PRIVATE UWORD tumble = 0x2000;
  46. static double t = 0;
  47.  
  48. #define FLUSH()  (*(WORD *)(0x41A) = *(WORD *)(0x41c))
  49.  
  50. #define SPL_DO(var) \
  51.     do { \
  52.         seg = CSP_FindSegment(spl_##var, t); \
  53.         var = CSP_Interpolate(spl_##var->pts + seg, t); \
  54.     } while (0)
  55.  
  56. PRIVATE int sign = 1;
  57.  
  58. void Advance(void) {
  59.     int seg;
  60.  
  61.     SPL_DO(x);
  62.     SPL_DO(y);
  63.     SPL_DO(z);
  64.     SPL_DO(rot);
  65.     SPL_DO(tumble);
  66.     z = z + 256;
  67.     x = sign*x;
  68.     if (sign < 0)
  69.         rot = 32768-rot;
  70.  
  71. /*
  72.     if (LLK_Keys[kKEYPADPLUS]) {
  73.         z += 0x10;
  74.     }
  75.     if (LLK_Keys[kKEYPADMINUS]) {
  76.         z -= 0x10;
  77.     }
  78.     if (LLK_Keys[kKEYPADSTAR]) {
  79.         theta += 0x10;
  80.     }
  81.     if (LLK_Keys[kKEYPADSLASH]) {
  82.         theta -= 0x10;
  83.     }
  84.     if (LLK_Keys[kKEYPAD4]) {
  85.         x -= 0x10;
  86.     }
  87.     if (LLK_Keys[kKEYPAD6]) {
  88.         x += 0x10;
  89.     }
  90.     if (LLK_Keys[kHOME]) {
  91.         tumble -= 0x100;
  92.     }
  93.     if (LLK_Keys[kEND]) {
  94.         tumble += 0x100;
  95.     }
  96.     if (LLK_Keys[kKEYPAD8]) {
  97.         y -= 0x10;
  98.     }
  99.     if (LLK_Keys[kKEYPAD2]) {
  100.         y += 0x10;
  101.     }
  102.     if (LLK_Keys[kKEYPAD5]) {
  103.         while (LLK_kbhit())
  104.             ;
  105.         VGA_SetMode(3);
  106.         printf("x: %d, y: %d, z: %d\n"
  107.                "rot: %d, theta: %d\n"
  108.                "press any key",
  109.                (int)x, (int)y, (int)z, (int)rot, (int)theta);
  110.         LLK_PressAnyKey();
  111.         VGA_SetMode(0x13);
  112.         VGA_DumpPalette(envpal, 0, 256);
  113.     }
  114.     if (LLK_Keys[k4]) {
  115.         theta = 16288;
  116.     }
  117.     if (LLK_Keys[k3]) {
  118.         theta = 21760;
  119.     }
  120.     if (LLK_Keys[k2]) {
  121.         theta = 32788;
  122.     }
  123.     if (LLK_Keys[k1]) {
  124.         theta = 0;
  125.     }
  126.     if (LLK_Keys[kT]) {
  127.         tracing ^= 1;
  128.         LLK_Keys[kT] = 0;
  129.         LLK_NumKeys--;
  130.     }
  131.     if (LLK_Keys[kS]) {
  132.         see_side ^= 1;
  133.         LLK_Keys[kS] = 0;
  134.         LLK_NumKeys--;
  135.     }
  136.     if (LLK_Keys[kA]) {
  137.         which_side += 0x200;
  138.     }
  139.     if (LLK_Keys[kD]) {
  140.         which_side -= 0x200;
  141.     }
  142.     if (LLK_NumKeys) {
  143.         FLUSH();
  144.     }
  145.     rot += theta;
  146. */
  147. }
  148.  
  149. TPH_Do(HANDLE obj) {
  150.  
  151.     Advance();
  152.     while (!LLK_SpacePressed) {
  153.         int nt;
  154.                 TRACE(32,0,0);
  155.         memset(virtscr, 0, 64000);              // Borrar fondo
  156.                 TRACE(0,32,0);
  157.         PH_XFormVertices(obj, rot, tumble, x, y, z);    // Girar vértices
  158.                 TRACE(0,0,32);
  159.         PH_XFormNormals(obj, rot, -tumble, 0x8000);      // Girar normales
  160.                 TRACE(32,32,0);
  161.         PH_DrawFrame();                         // Dibujar
  162.                 TRACE(0,0,0);
  163.         nt = VBL_VSync(2);                     // Esperamos para volcar
  164.         if (t < 630 && (nt+t) >= 630)
  165.             break;
  166.         t += nt;
  167.                 TRACE(32,0,32);
  168.         DumpScr();
  169.                 TRACE(0,0,0);
  170.         Advance();
  171.         if (LLK_Keys[kT]) {
  172.             tracing ^= 1;
  173.             LLK_Keys[kT] = 0;
  174.             LLK_NumKeys--;
  175.         }
  176.     }
  177.     FLUSH();
  178. }
  179.  
  180. CSP_PSpline LoadSpline(char *fname) {
  181.     FILE *fd;
  182.     char buf[256];
  183.     int i, n;
  184.     CSP_PSpline spline;
  185.  
  186.     fd = JCLIB_OpenText(fname);
  187.     if (!fd)
  188.         return NULL;
  189.     fgets(buf, 256, fd);
  190.     n = atoi(buf);
  191.     if (n <= 0)
  192.         return NULL;
  193.     spline = NEW(CSP_Size(n));
  194.     if (!spline)
  195.         return NULL;
  196.     memset(spline, 0, CSP_Size(n));
  197.     spline->npts = n;
  198.  
  199.     i = 0;
  200.     for (;;) {
  201.         fgets(buf, 256, fd);
  202.         while (strlen(buf) && isspace(buf[strlen(buf)-1])) {
  203.             buf[strlen(buf)-1] = '\0';
  204.         }
  205.         if (!strcmp(buf, "EOF"))
  206.             break;
  207.         if (!strlen(buf))
  208.             continue;
  209.         spline->pts[i].f = atof(buf);
  210.         fgets(buf, 256, fd);
  211.         while (strlen(buf) && isspace(buf[strlen(buf)-1])) {
  212.             buf[strlen(buf)-1] = '\0';
  213.         }
  214.         spline->pts[i].t = atof(buf);
  215.         i++;
  216.     }
  217.     JCLIB_Close(fd);
  218.     if (i != n) {
  219.         DISPOSE(spline);
  220.         return NULL;
  221.     }
  222.     return spline;
  223. }
  224.  
  225. PUBLIC int DoPhong(void) {
  226.     HANDLE obj, ob2;
  227.  
  228.     memset(LLS_Screen[0], 0, LLS_Size);
  229.     LLS_Update();
  230.  
  231.     spl_x = LoadSpline("x.spl");
  232.     spl_y = LoadSpline("y.spl");
  233.     spl_z = LoadSpline("z.spl");
  234.     spl_rot = LoadSpline("rot.spl");
  235.     spl_tumble = LoadSpline("tumble.spl");
  236.     if (!spl_x || !spl_y || !spl_z || !spl_rot || !spl_tumble) {
  237.         BASE_Abort("Load Splines");
  238.     }
  239.     CSP_CalcSpline(spl_x);
  240.     CSP_CalcSpline(spl_y);
  241.     CSP_CalcSpline(spl_z);
  242.     CSP_CalcSpline(spl_rot);
  243.     CSP_CalcSpline(spl_tumble);
  244.  
  245.     // Carga el environment map
  246.     envpix = fload("envmap.pix");
  247.     envpal = fload("envmap.pal");
  248.     if (!envpix || !envpal) {
  249.         puts("Cannot find envmap.pix | envmap.pal");
  250.         return -1;
  251.     }
  252.     env_map_adr = envpix;
  253.  
  254.     // Carga el objeto
  255.     o3d = fload("test.o3d");
  256.     if (!o3d) {
  257.         puts("Cannot find o3d file");
  258.         return -1;
  259.     }
  260.     obj = PH_BuildObj(o3d);
  261.     if (!obj) {
  262.         puts("Not enough mem to build object in mem");
  263.         return -1;
  264.     }
  265.     free(o3d);
  266.  
  267.     // Carga el objeto
  268.     o3d = fload("face.o3d");
  269.     if (!o3d) {
  270.         puts("Cannot find o3d file");
  271.         return -1;
  272.     }
  273.     ob2 = PH_BuildObj(o3d);
  274.     if (!ob2) {
  275.         puts("Not enough mem to build object in mem");
  276.         return -1;
  277.     }
  278.     free(o3d);
  279.  
  280.     // Virtual screen
  281.     virtscr = LLS_Screen[0];
  282.     PH_SetMode13BufferAdr(virtscr);
  283.  
  284.     // Init-ea las distintas cosas
  285.     if (!PH_StartEngine()) {
  286.         puts("not enough mem for triangle tables!");
  287.         return -1;
  288.     }
  289.  
  290.     while (!LLK_SpacePressed && DVTInfo != NULL && !DVTInfo->semaphores[3]);
  291.  
  292.  
  293.     VBL_FadeSpeed = VBL_FadePos = 0;
  294.     VBL_DumpPalette(envpal, 0, 256);
  295.     VBL_VSync(0);
  296.     PH_ActivateObj(ob2, 0);
  297.     TPH_Do(obj);
  298.     PH_ActivateObj(ob2, 1);
  299.     PH_ActivateObj(obj, 0);
  300.     sign = -1;
  301.     t = 0;
  302.     TPH_Do(ob2);
  303.  
  304.     PH_DestroyObj(obj);
  305.     PH_DestroyObj(ob2);
  306.     PH_EndEngine();
  307.  
  308.     free(envpix);
  309.     free(envpal);
  310.     return 0;
  311. }
  312.